import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.expected_conditions import alert_is_present
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import BlogDriver


class BlogLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.driver = BlogDriver.driver
        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver.get(self.url)

    #登录成功测试
    def loginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #找到三个模块才算正确登录
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > img")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.title")
        #BlogDriver.getScreenshot()


    #登录失败测试,空白，姓名或密码错误
    def loginFailTest(self):
        #为弹窗设置一个显示等待
        wait = WebDriverWait(self.driver,2)
        #全空白
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        wait.until(alert_is_present())
        alter = self.driver.switch_to.alert
        alter.accept()

        #密码空白
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        wait.until(alert_is_present())
        alter = self.driver.switch_to.alert
        alter.accept()

        # 账号空白
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        wait.until(alert_is_present())
        alter = self.driver.switch_to.alert
        alter.accept()

        #密码错误
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        wait.until(alert_is_present())
        alter = self.driver.switch_to.alert
        alter.accept()

        # 账号错误
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsang")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        wait.until(alert_is_present())
        alter = self.driver.switch_to.alert
        alter.accept()

    # 注销登录
    def logoutBlog(self):
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").click()
        title = self.driver.title
        assert title == "博客登陆页"
        BlogDriver.getScreenshot()

